<template>
  <div class="news">
    <banner title="新闻资讯" />
    <div class="news-section" v-loading="loading">
      <div class="news-section-content">
        <div class="content-nav">
          <div class="content-nav-btn" :class="{ 'content-nav-active': newsType == 1 }" @click="newsType = 1">
            <span>公司新闻</span>
          </div>
          <div class="content-nav-btn" :class="{ 'content-nav-active': newsType == 2 }" @click="newsType = 2">
            <span>行业动态</span>
          </div>
        </div>

        <TransitionGroup name="slide-up" appear tag="div" class="content-nav-item">
          <div class="item-list" :key="item.id" v-for="item in newsListByType[newsType]">
            <router-link class="text-decoration" :to="{ name: 'newsDetails', params: { id: item.id } }">
              <img :src="getFirstImageUrl(item.content)" alt />
              <p class="item-list-title">{{ item.title }}</p>
            </router-link>
          </div>
        </TransitionGroup>
      </div>
    </div>
  </div>
</template>

<script>
import Banner from "../components/Banner";
export default {
  name: "news",
  components: {
    Banner,
  },
  data() {
    return {
      loading: true,
      newsListByType: { 1: [], 2: [] },
      newsType: 1,
    };
  },
  methods: {
    getFirstImageUrl(content) {
      const imgRegex = /<img[^>]+src="([^">]+)"/;
      const match = content.match(imgRegex);
      return match ? match[1] : "";
    },
    loadData() {
      this.loading = true;
      this.$http
        .get(`news/all`)
        .then((response) => {
          // 按时间排序
          const sortedData = response.data.sort((a, b) => {
            return new Date(b.createTime) - new Date(a.createTime);
          });

          this.newsListByType = {
            1: sortedData.filter((e) => e.type == 1),
            2: sortedData.filter((e) => e.type == 2),
          };
          this.loading = false;
          // 手动触发入场动画
          const itemLists = document.querySelectorAll(".item-list");
          itemLists.forEach((itemList) => {
            itemList.classList.add("slide-up-enter-active");
          });
          //window.console.log(this.newsList);
        })
        .catch(function (error) {
          window.console.log(error);
        });
    },
  },
  mounted() {
    this.loadData();
  },
  created() {
    [...document.getElementsByClassName("item-list")].forEach((e) => {
      e.classList.add("slideUp");
    });
  },
};
</script>

<style lang="scss" scoped>
@keyframes slideUpAnimation {
  0% {
    transform: translateY(50px);
    opacity: 0;
  }

  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes imgZoom {
  0% {
    transform: scale(1);
  }

  100% {
    transform: scale(1.05); // 这里设置放大到原来的1.2倍，你可以根据需求调整这个倍数
  }
}

* {
  margin: 0;
  padding: 0;
}

.news {
  background: #f5f5f5;
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;

  &-section {
    width: 100%;
    //height: 1600px;
    background: #f5f5f5;

    &-content {
      //height: 1600px;
      margin: 0 auto;

      .content-nav {
        width: 40vw;
        height: 55px;
        margin: 0 auto;
        display: flex;
        //justify-content: center;
        align-items: center;
        position: relative;
        bottom: 30px;

        &-btn {
          width: 50%;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          background-color: #e4e4e4;
          transition: all 0.3s ease-out; // 添加过渡属性，设置过渡时间为0.3秒，缓动效果为ease-out

          &.content-nav-active {
            background-color: #001529;
            color: #fff;
          }
        }
      }

      .content-nav-item {
        justify-content: space-around;
        margin: 0 5vw;
        margin-bottom: 5%;
        display: flex;
        flex-wrap: wrap;

        //border: 1px solid blue;
        .item-list {
          width: 70vw;
          display: flex;
          flex-direction: column;
          margin-bottom: 20px;

          // 以下是与Transition组件配合的动画类定义
          &.slide-up-enter-active {
            animation: slideUpAnimation 1s ease-out;
          }

          &.slide-up-leave-active {
            animation: slideUpAnimation 1s ease-out reverse;
          }

          &.slide-up-enter,
          &.slide-up-leave-to {
            transform: translateY(50px);
            opacity: 0;
          }

          &:hover {
            animation: imgZoom 0.3s ease-out;
            animation-fill-mode: forwards;
          }

          // 鼠标移开时恢复到原始状态，通过Transition组件来管理动画状态，避免冲突
          &.slide-up-leave-to:not(:hover) {
            transform: translateY(0);
            opacity: 1;
            box-shadow: none;
            animation: none;
          }

          img {
            width: 70vw;
          }

          &-title {
            color: #000;
            font-size: 18px;
            margin: 1vw 0;
            white-space: normal;
            box-sizing: border-box; // 添加box-sizing: border-box确保内边距和边框不会影响宽度计算
            max-width: 100%; // 确保标题不会超出父容器宽度
          }
        }

        @media screen and (max-width: 1000px) {
          .item-list {
            width: 88vw;

            img {
              width: 88vw;
            }

            &-title {
              font-size: 16px;
            }
          }
        }
      }
    }
  }

  .text-decoration {
    text-decoration: none;
  }
}
</style>